/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProfesoresFrame.java
 *
 * Created on 02-ene-2012, 17:09:49
 */
package occho.view;

import java.sql.SQLException;
import java.util.List;
import java.util.Vector;
import occho.model.FachadaAdministrador;
import occho.model.FachadaRegistro;
import occho.model.Usuario;
import occho.view.resources.Messages;

/**
 *
 * @author Jorge
 */
public class ProfesoresFrame extends javax.swing.JInternalFrame implements Internacionalizable {
    
    private FachadaAdministrador fachada = null;
    private FachadaRegistro fachadaRegistro = null;
    
    private Usuario usuario = null;

    /** Creates new form ProfesoresFrame
     * @param admin 
     */
    public ProfesoresFrame(Usuario admin) {
        initComponents();
        setTitle("Profesores");
        fachada = new FachadaAdministrador(admin);
        fachadaRegistro = new FachadaRegistro();
        
        actualizarListaProfesores();
        setLenguaje();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        profesoresList = new javax.swing.JList();
        btnCrear = new javax.swing.JButton();
        btnModificar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        crearProfesorPanel = new javax.swing.JPanel();
        lbNombre = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        lbApellidos = new javax.swing.JLabel();
        txtApellidos = new javax.swing.JTextField();
        lbEmail = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        lbPassword = new javax.swing.JLabel();
        txtPassword = new javax.swing.JPasswordField();

        setClosable(true);
        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        profesoresList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        profesoresList.setName("profesoresList"); // NOI18N
        profesoresList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                profesoresListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(profesoresList);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(occho.OcchoApp.class).getContext().getResourceMap(ProfesoresFrame.class);
        btnCrear.setText(resourceMap.getString("btnCrear.text")); // NOI18N
        btnCrear.setName("btnCrear"); // NOI18N
        btnCrear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCrearActionPerformed(evt);
            }
        });

        btnModificar.setText(resourceMap.getString("btnModificar.text")); // NOI18N
        btnModificar.setName("btnModificar"); // NOI18N
        btnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarActionPerformed(evt);
            }
        });

        btnEliminar.setText(resourceMap.getString("btnEliminar.text")); // NOI18N
        btnEliminar.setName("btnEliminar"); // NOI18N
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        crearProfesorPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("crearProfesorPanel.border.title"))); // NOI18N
        crearProfesorPanel.setName("crearProfesorPanel"); // NOI18N

        lbNombre.setText(resourceMap.getString("lbNombre.text")); // NOI18N
        lbNombre.setName("lbNombre"); // NOI18N

        txtNombre.setText(resourceMap.getString("txtNombre.text")); // NOI18N
        txtNombre.setName("txtNombre"); // NOI18N

        lbApellidos.setText(resourceMap.getString("lbApellidos.text")); // NOI18N
        lbApellidos.setName("lbApellidos"); // NOI18N

        txtApellidos.setText(resourceMap.getString("txtApellidos.text")); // NOI18N
        txtApellidos.setName("txtApellidos"); // NOI18N

        lbEmail.setText(resourceMap.getString("lbEmail.text")); // NOI18N
        lbEmail.setName("lbEmail"); // NOI18N

        txtEmail.setText(resourceMap.getString("txtEmail.text")); // NOI18N
        txtEmail.setName("txtEmail"); // NOI18N

        lbPassword.setText(resourceMap.getString("lbPassword.text")); // NOI18N
        lbPassword.setName("lbPassword"); // NOI18N

        txtPassword.setText(resourceMap.getString("txtPassword.text")); // NOI18N
        txtPassword.setName("txtPassword"); // NOI18N

        org.jdesktop.layout.GroupLayout crearProfesorPanelLayout = new org.jdesktop.layout.GroupLayout(crearProfesorPanel);
        crearProfesorPanel.setLayout(crearProfesorPanelLayout);
        crearProfesorPanelLayout.setHorizontalGroup(
            crearProfesorPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(crearProfesorPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(crearProfesorPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(crearProfesorPanelLayout.createSequentialGroup()
                        .add(lbNombre)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 69, Short.MAX_VALUE)
                        .add(txtNombre, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 220, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(crearProfesorPanelLayout.createSequentialGroup()
                        .add(lbApellidos)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 60, Short.MAX_VALUE)
                        .add(txtApellidos, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 220, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(crearProfesorPanelLayout.createSequentialGroup()
                        .add(lbEmail)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 85, Short.MAX_VALUE)
                        .add(txtEmail, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 220, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(crearProfesorPanelLayout.createSequentialGroup()
                        .add(lbPassword)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 48, Short.MAX_VALUE)
                        .add(txtPassword, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 220, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        crearProfesorPanelLayout.setVerticalGroup(
            crearProfesorPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(crearProfesorPanelLayout.createSequentialGroup()
                .add(crearProfesorPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lbNombre)
                    .add(txtNombre, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(crearProfesorPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lbApellidos)
                    .add(txtApellidos, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(crearProfesorPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lbEmail)
                    .add(txtEmail, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(crearProfesorPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lbPassword)
                    .add(txtPassword, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, crearProfesorPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 391, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(btnEliminar)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(btnModificar)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(btnCrear)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(crearProfesorPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btnModificar)
                    .add(btnEliminar)
                    .add(btnCrear))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void profesoresListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_profesoresListValueChanged
    if (profesoresList.getSelectedIndex() != -1) {
        usuario = (Usuario) profesoresList.getSelectedValue();
    }
    else {
        usuario = null;
    }
    mostrarUsuario();
}//GEN-LAST:event_profesoresListValueChanged

private void btnCrearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCrearActionPerformed
    if (validarUsuario()) {
        try {
            if (fachadaRegistro.registrarUsuario(txtNombre.getText(), txtApellidos.getText(), 
                    txtEmail.getText(), new String(txtPassword.getPassword()), Usuario.PROFESOR)) {
                usuario = null;
                mostrarUsuario();
                actualizarListaProfesores();
            }
        } catch (ClassNotFoundException ex) {
            System.out.println("Class_Not_Found_Exception: " + ex.getMessage());
        } catch (SQLException ex) {
            System.out.println("SQL_Exception: " + ex.getMessage());
        }
    }
}//GEN-LAST:event_btnCrearActionPerformed

private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
    if (usuario != null) {
        try {
            if (fachada.eliminarUsuario(usuario)) {
                usuario = null;
                mostrarUsuario();
                actualizarListaProfesores();
            }
        } catch (ClassNotFoundException ex) {
            System.out.println("Class_Not_Found_Exception: " + ex.getMessage());
        } catch (SQLException ex) {
            System.out.println("SQL_Exception: " + ex.getMessage());
        }
    }
}//GEN-LAST:event_btnEliminarActionPerformed

private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarActionPerformed
    if (usuario != null && validarUsuario()) {
        usuario.setNombre(txtNombre.getText());
        usuario.setApellidos(txtApellidos.getText());
        usuario.setEmail(txtEmail.getText());
        usuario.setPassword(new String(txtPassword.getPassword()));
            try {
                if (fachada.modificarProfesor(usuario)) {
                    usuario = null;
                    mostrarUsuario();
                    actualizarListaProfesores();
                }
            } catch (ClassNotFoundException ex) {
                System.out.println("Class_Not_Found_Exception: " + ex.getMessage());
            } catch (SQLException ex) {
                System.out.println("SQL_Exception: " + ex.getMessage());
            }
    }
}//GEN-LAST:event_btnModificarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCrear;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnModificar;
    private javax.swing.JPanel crearProfesorPanel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbApellidos;
    private javax.swing.JLabel lbEmail;
    private javax.swing.JLabel lbNombre;
    private javax.swing.JLabel lbPassword;
    private javax.swing.JList profesoresList;
    private javax.swing.JTextField txtApellidos;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JPasswordField txtPassword;
    // End of variables declaration//GEN-END:variables

    /**
     * Actualiza la lista de los profesores
     */
    @SuppressWarnings("UseOfObsoleteCollectionType")
    private void actualizarListaProfesores() {
        try {
            List<Usuario> profesores = fachada.seleccionarProfesores();
            profesoresList.setListData((Vector<Usuario>) profesores);
        } catch (ClassNotFoundException ex) {
            System.out.println("Class_Not_Found_Exception: " + ex.getMessage());
        } catch (SQLException ex) {
            System.out.println("SQL_Exception: " + ex.getMessage());
        }
    }
    
    /**
     * Comprueba si los datos del usuario están completos
     * @return true si los datos están completos
     */
    private boolean validarUsuario() {
        String pass = new String(txtPassword.getPassword());
        if (txtNombre.getText().isEmpty() || txtApellidos.getText().isEmpty() ||
                txtEmail.getText().isEmpty() || pass.isEmpty()) {
            return false;
        }
        return true;
    }
    
    /**
     * Muestra los datos del usuario seleccionado
     */
    private void mostrarUsuario() {
        if (usuario != null) {
            txtNombre.setText(usuario.getNombre());
            txtApellidos.setText(usuario.getApellidos());
            txtEmail.setText(usuario.getEmail());
            txtPassword.setText(usuario.getPassword());
        }
        else {
            txtNombre.setText("");
            txtApellidos.setText("");
            txtEmail.setText("");
            txtPassword.setText("");
        }
    }

    /**
     * Establece lenguaje
     */
    public void setLenguaje() {
        setTitle(Messages.getString("Form.title", Messages.BUNDLE_NAME_PROFESORES));
        btnCrear.setText(Messages.getString("btnCrear.text", Messages.BUNDLE_NAME_PROFESORES));
        btnEliminar.setText(Messages.getString("btnEliminar.text", Messages.BUNDLE_NAME_PROFESORES));
        btnModificar.setText(Messages.getString("btnModificar.text", Messages.BUNDLE_NAME_PROFESORES));
        crearProfesorPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(Messages.getString("crearProfesorPanel.border.title", Messages.BUNDLE_NAME_PROFESORES)));
        lbApellidos.setText(Messages.getString("lbApellidos.text", Messages.BUNDLE_NAME_PROFESORES));
        lbEmail.setText(Messages.getString("lbEmail.text", Messages.BUNDLE_NAME_PROFESORES));
        lbNombre.setText(Messages.getString("lbNombre.text", Messages.BUNDLE_NAME_PROFESORES));
        lbPassword.setText(Messages.getString("lbPassword.text", Messages.BUNDLE_NAME_PROFESORES));
    }

}
